//
//  YMUser.swift
//  Driver
//
//  Created by lantaiyuan on 2017/11/3.
//  Copyright © 2017年 youmy. All rights reserved.
//

import UIKit

fileprivate let accountFile:NSString = "useraccount.json"

class User_root: NSObject {
    var success: Bool = false
    var msg: String?
    var statusCode: Int64 = 0
    var obj: User_obj?
}

class User_obj: NSObject {
    var userId: String?
    var headImageUrl: String?
    var username: String?
    var birthday: String?
    var mobilephone: String?
    var cityCode: String?
    var cityName: String?
    var accountStatus: Int64 = 0
    var busSeats: Int64 = 0
    var vehicleId: String?
    var token: String?
    
    func savaUserAccount() {
        // 1. 模型转字典
        let dict = self.modelToJSONObject() as? [String: AnyObject] ?? [:]
        
        // 2. 字典序列化 data
        guard let data = try?JSONSerialization.data(withJSONObject: dict, options: []),
            let filePath = accountFile.cz_appendDocumentDir()  else {
                return
        }
        
        // 3. 写入磁盘
        (data as NSData).write(toFile: filePath, atomically: true)
        
        print("用户帐户保存成功 \(filePath)")
    }
    
    class func account() -> User_obj? {
        let path = Bundle.main.path(forResource: "order.json", ofType: nil) ?? ""
        guard
            let data = try? Data.init(contentsOf: URL(fileURLWithPath: path)),
            let dic = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! [String: Any],
            let user = User_obj.model(withJSON: dic) else {
                return nil
        }
        
        return user
    }
}
